package com.aube.mis.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aube.mis.dto.LotteryDTO;
import com.aube.mis.dto.LotteryOptionDTO;
import com.aube.mis.dto.LotteryWinnerDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.RelatedCallbackDTO;
import com.aube.mis.dto.ShowModelDTO;
import com.aube.mis.service.LotteryService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/lottery")
public class LotterryController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(LotterryController.class);

	@Resource
	LotteryService lotteryService;
	
	@Resource
	ShowSettingService showSettingService;

	@RequestMapping(value = "/list")
	public String lotteryList(HttpServletResponse response, ModelMap map, @ModelAttribute RelatedCallbackDTO relatedCallbackDTO) {
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		return "/lottery/list";
	}

	@RequestMapping(value = "/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String lotteryData(@ModelAttribute DataTableCriterias criterias) {
		criterias.setLength(9999);
		List<LotteryDTO> list = lotteryService.getLotteryList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", list);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String lotteryAdd(HttpServletResponse response, ModelMap map) throws Throwable {
		return "/lottery/add.page";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String lotteryAdd(HttpServletResponse response, ModelMap map, String activityId) throws Throwable {
		LotteryDTO lotteryDetail = lotteryService.getLotteryDetail(activityId);
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		map.put("lottery", lotteryDetail);
		map.put("lotteryOptionJson", JsonUtil.toJson(lotteryDetail.getOptionList()));
		return "/lottery/edit";
	}

	@RequestMapping(value = "/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String lotteryEditAjax(@ModelAttribute LotteryDTO lotteryDTO) throws Throwable {
		if(CollectionUtils.isNotEmpty(lotteryDTO.getOptionList())) {
			for (Iterator<LotteryOptionDTO> iterator = lotteryDTO.getOptionList().iterator(); iterator.hasNext();) {
				LotteryOptionDTO option = iterator.next();
				if (option == null || StringUtils.isEmpty(option.getDisplayName())) {
					iterator.remove();
				}
			}
		}
		LotteryDTO lottery = lotteryService.editLottery(lotteryDTO);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("lottery", lottery);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String lotteryDelete(String lotteryId) throws Throwable {
		lotteryService.deleteLottery(lotteryId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public String lotterySelect(HttpServletResponse response, ModelMap map) throws Throwable {
		List<LotteryDTO> lotteryList = lotteryService.getLotteryList(new DataTableCriterias(0, 9999), getShowId());
		map.put("lotteryList", lotteryList);
		return "/lottery/select.page";
	}

	@RequestMapping(value = "/winner/list")
	public String lotteryWinnerList(HttpServletResponse response, ModelMap map, String activityId) {
		map.put("activityId", activityId);
		return "/lottery/winner/list";
	}

	@RequestMapping(value = "/winner/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String lotteryWinnerData(@ModelAttribute DataTableCriterias criterias, String activityId) {
		PaginationResultDTO<LotteryWinnerDTO> list = lotteryService.getWinnerList(criterias, getShowId(), activityId);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", list.getResultList());
		dataMap.put("total", list.getTotal());
		return this.createSuccessResponse(dataMap);
	}
}
